﻿using System.Net;
using System.Net.Sockets;
using KestrelClientWithSuperSocket;
using SuperSocket.Client;
using SuperSocket.IOCPEasyClient;

var client =
    new IOCPEasyClient<EachPackage, EachPackage>(new EachPipeLineFilter(), new EachPackageEncode()).AsClient();

client.Closed += OnClosed;
client.PackageHandler += OnPackageHandler;

Console.WriteLine("正在连接");

var connected = await client.ConnectAsync(new DnsEndPoint("127.0.0.1", 5006, AddressFamily.InterNetwork));

Console.WriteLine($"连接结果: {connected}");

client.StartReceive();

while (true)
{
    await client.SendAsync(new EachPackage
    {
        Content = "heartBeat"
    });
    
    await Task.Delay(1000);
}

Console.ReadKey();

void OnClosed(object? sender, EventArgs e)
{
    Console.WriteLine("断开连接");
}

ValueTask OnPackageHandler(IEasyClient<EachPackage>? client, EachPackage packet)
{
    Console.WriteLine($"content：{packet.Content}");

    return ValueTask.CompletedTask;
}